Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: '点击按时间筛选',
    adviceList: [],
    alladviceList:[],
    isNull: false,
  },


  /* 筛选所有 */
  loadAll: function () {

    this.setData({
      adviceList: null,
      isNull: false,
      date: '点击按时间筛选'
    })
    wx.showLoading({
      title: '载入数据中',
    })

    if(this.data.alladviceList.length > 0){
      wx.hideLoading()
      this.setData({
        adviceList: this.data.alladviceList
      })
    }else{
      wx.hideLoading()
      this.setData({
        isNull: true
      })
    }
  },


  //  点击日期组件确定事件  
  bindDateChange: function (e) {
    wx.showLoading({
      title: '载入数据中',
    }) 

    var that = this;
    let data = that.data.alladviceList;

    that.setData({
      date: e.detail.value,
      adviceList: null,
      isNull: false
    });

  
    //筛选需要的数据
    if(data){
      let advieceList = [];
      for (let i = 0; i < data.length; i++) {
        let time = data[i]['createTime'];
        console.log(/\//.test(time))
        let changeTime =  time.replace(/\//g, "-").substr(0,10);
        console.log(e.detail.value, changeTime)
        if(e.detail.value === changeTime){
          advieceList.push(data[i])
        }
      }
      wx.hideLoading();
      console.log(advieceList)
      if(advieceList.length > 0){
        that.setData({
          isNull: false,
          adviceList:advieceList
        })
      }else{
        that.setData({
          isNull: true
        })
      }
    }else{
      wx.hideLoading();
      that.setData({
        isNull: true
      })
    }
    
    
  },

  /* 去详情页面 */
  detailPanel: function (options) {
    wx.navigateTo({
      url: '/pages/admAdviceListDetail/admAdviceListDetail?id=' + options.currentTarget.dataset.id,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '载入数据中',
    })
    var openId = getApp().globalData.userInfo._openid;
    wx.cloud.database().collection("advice").get().then(res => {
      wx.hideLoading();
      let data = res.data;
      console.log(data);
      if (data.length > 0) {
        this.setData({
          adviceList: data,
          alladviceList : data
        })
      } else {
        this.setData({
          isNull: true
        })
      }
    })
  },


})